package com.woniu.cloud.storeassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TStoreLogin;
import com.woniu.cloud.storeassembly.mapper.TStoreLoginMapper;
import com.woniu.cloud.storeassembly.service.ITStoreLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 店铺登录表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-29
 */
@Service
@Slf4j
public class TStoreLoginServiceImpl extends ServiceImpl<TStoreLoginMapper, TStoreLogin> implements ITStoreLoginService {


    @Resource
    private TStoreLoginMapper storeLoginMapper;

    @Override
    public Result getStoreLoginByLoginId(String username, String password) {
        QueryWrapper<TStoreLogin> wrapper = new QueryWrapper<>();
        wrapper.eq("store_login_username",username)
                .eq("store_login_password",password);
        List<TStoreLogin> list = storeLoginMapper.selectList(wrapper);
        if (0!=list.size()) {
            return Result.success("账号存在",list.get(0));
        }
        return Result.fail("账号不存在",null);
    }
    @Override
    public Result insertStoreLogin(TStoreLogin storeLogin) {
        QueryWrapper<TStoreLogin> wrapper = new QueryWrapper<>();
        wrapper.eq("store_login_username",storeLogin.getStoreLoginUsername());
        List<TStoreLogin> list = storeLoginMapper.selectList(wrapper);
        if (0!=list.size()) {
            return Result.fail("姓名存在",null);
        }
        int insert = storeLoginMapper.insert(storeLogin);
        if (0!=insert) {
            log.info("添加成功->{}",storeLogin);
            return Result.success("添加成功",null);
        }
        return Result.fail("添加失败",null);

    }

    @Override
    public Result updateStoreLoginByStoreLoginId(TStoreLogin storeLogin) {
        QueryWrapper<TStoreLogin> wrapper = new QueryWrapper<>();
        wrapper.eq("store_login_username",storeLogin.getStoreLoginUsername());
        List<TStoreLogin> list = storeLoginMapper.selectList(wrapper);
        if (0!=list.size()) {
            return Result.fail("姓名存在",null);
        }
        UpdateWrapper<TStoreLogin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("store_login_id",storeLogin.getStoreLoginId())
                .set(null!=storeLogin.getStoreLoginUsername()&&""!=storeLogin.getStoreLoginUsername(),"store_login_username",storeLogin.getStoreLoginUsername())
                .set(null!=storeLogin.getStoreLoginPassword()&&""!=storeLogin.getStoreLoginPassword(),"store_login_password",storeLogin.getStoreLoginPassword());
        int update = storeLoginMapper.update(null, updateWrapper);
        if (0!=update) {
            log.info("修改成功->{}");
            return Result.success("修改成功",null);
        }
        return Result.fail("修改失败",null);
    }

    @Override
    public Result deleteStoreLoginByStoreLoginId(Integer storeLoginId) {
        log.info("正在删除->storeLoginId:{}",storeLoginId);
        int isDelete = storeLoginMapper.deleteById(storeLoginId);
        if (1==isDelete) {
            log.info("删除成功->");
            return Result.success("删除成功",null);
        }
        log.info("删除失败->");
        return Result.fail("删除失败",null);

    }
}
